<?php 
App::import('Model', 'Cms.CmsAction');
class ModuleVisibility extends CmsAction
{
	var $name = 'ModuleVisibility';
	
	var $belongsTo = array(
        'Module' => array(
			'class' =>'Modules.Module',
        	'counterCache' => true
		)
    );
    
    var $hasMany = array(
    	'Modules.ModuleVisibilityParam'
    );
    
    function initialize()
    {
    	$this->fieldProperties['filter_value']['storageModel'] = array('Modules.ModuleVisibilityParam', 'value');
    	//prd($this->fieldProperties);
    }
    
    function beforeSave()  // kinda hack
    {
    	if(strlen($this->data['ModuleVisibility']['action_id']) < 1)
    	{
    		return false;
    	}
    	if(isset($this->data['ModuleVisibility']['filter']))
    	{
    		if($this->data['ModuleVisibility']['filter'] == '')
    		{
    			$this->data['ModuleVisibility']['filter'] = null;
    		}
    	}
    	return true;
    }
    
    /*
    function afterFind1($results)
    {
    	if(isset($results[0]['ModuleVisibility'][0]))
    	{
    		// reverse what we did in beforeChildrenSave()
    		foreach($results[0]['ModuleVisibility'] as $key => $record)
    		{
    			$results[0]['ModuleVisibility'];
    		}
    	}
    }
    
    function beforeChildrenSave($data)
    {
    	$newData = array();
		foreach($data as $key => $rule)
		{
			if(isset($rule['filter']))
			{
				//list($action_id, $filter) = explode('--', $rule['filter']);
				//$rule['filter'] = $filter;

				if(is_array($rule['filter_value']))
				{
					foreach($rule['filter_value'] as $key => $param)
					{
						$rule['filter_value'] = $param;
						array_push($newData, $rule);
					}
				}
			}
			else
			{
				unset($rule['filter_value']);
				array_push($newData, $rule);
			}
			
		}
		return $newData;
    }*/
	
	function _getActionSpecificModules($action)
	{
		$cacheKey = 'module_';
		
		$conditions = array();
		$actionId = $action['action_id'];
		$cacheKey .= $actionId;

		// 1. fetch wildcard (no filter) module ids
		$condition1 = array(
			'ModuleVisibility.action_id' => $actionId, 
			'ModuleVisibility.filter' => null
		);
		$moduleIds = $this->find('all', array('conditions' => $condition1, 'fields' => 'module_id'));
		$moduleIds_wildcard = Set::extract('{n}.ModuleVisibility.module_id', $moduleIds);
		//pr($moduleIds_wildcard);
		$joins = array();
		
		
		// 2. fetch limited visibility module ids
		$moduleIds_actionSpecfic = array();
		$condition2 = array();
		if(isset($action['params']))
		{
			foreach($action['params'] as $filter => $val)
			{
				$joins = array(array(
					'table'=>'module_visibility_params',
					'alias'=>'ModuleVisibilityParam',
					'type' =>'inner',
					'conditions' =>array('ModuleVisibility.id = ModuleVisibilityParam.module_visibility_id')
				));
				$condition2 = array(
					'ModuleVisibility.action_id' => $action['action_id'], 
					'ModuleVisibility.filter' => $action['action_id'].'--'.$filter,
					'ModuleVisibilityParam.value' => $val,
				);
				$cacheKey .= '_'.$filter.'_'.$val;
			}
			$conditions = $condition2;

			$fields = array('module_id');

			$this->recursive = -1;
			$moduleIds = $this->find('all', compact('conditions', 'fields', 'joins'));
			$moduleIds_actionSpecfic = Set::extract('{n}.ModuleVisibility.module_id', $moduleIds);
		}		
		$moduleIds = array_unique(array_merge($moduleIds_wildcard, $moduleIds_actionSpecfic));

		return $moduleIds;
	}
}


?>